<?php

namespace app\api\controller;

use app\admin\model\SystemBanner;
use app\admin\model\SystemCate;
use app\admin\model\SystemGame;
use app\admin\model\SystemPlatform;
use app\api\model\Comment;
use app\api\model\Follow;
use app\api\model\PictureCollection;
use app\api\model\PictureLike;
use app\common\controller\ApiController;
use think\App;
use app\api\model\Picture;
use think\Request;

class Index extends ApiController
{

    /**
     * 画列表
     * @return string
     * @throws \Exception
     */

    public function index(){

       
        return redirect('/Zhaopin/index');
    }
    public function picList(Request $request)
    {
        $keyword = $request->param('keyword');
        $page = $request->param('page', 1);
        $pageSize = $request->param('pageSize',10);
        $cat_id = $request->param('cat_id');
        $order = $request->param('order');
        $where = [];
        if(isset($keyword) && $keyword !=''){
            $where[] = ['title', 'like',"%".$keyword.'%'];
        }
        if(isset($cat_id) && $cat_id !=0){
            $where[] = ['cat_id', '=', $cat_id];
        }
        $list = Picture::where($where)->with(['user'=>function($q){
            $q->field('id,nickname,avatar');
        }])->withCount(['like']);
        if(isset($order) && $order =='time'){
            $list = $list->order('create_time','desc');
        }
        if(isset($order) && $order =='zan'){
            $list = $list->order('like_count','desc');
        }
        $list = $list->paginate($pageSize);

        $this->success('',$list);
    }

    public function picDetail(Request $request)
    {
        $user = $this->getUserId();

        $pid = $request->param('pid');
        $detail = Picture::where('id', $pid)->with(['like','user'=>function($q){
            $q->field('id,nickname,avatar');
        },'picTag'=>function($q){
            $q->with(['tag']);
        }])->withCount(['like'=>function($q)use($user){
            $q->where('user_id', $user['id']);
        }, 'picCollection'=>function($q)use($user){
            $q->where('user_id', $user['id']);
        }])->find();
        $comment = Comment::where('pid', $pid)->select()->toArray();
        foreach($comment as $k=>$v){
            if($v['user_id'] == $user['id']){
                $comment[$k]['is_self'] = 1;
            }else{
                $comment[$k]['is_self'] = 0;
            }
        }
        $is_follow = Follow::where('user_id', $user['id'])->where('follow_user_id', $detail['user_id'])->find();
        if($is_follow){
            $detail['is_follow'] = 1;
        }else{
            $detail['is_follow'] = 0;
        }
        $comment_tree = getTree($comment);
        $detail['comment'] = $comment_tree;
        $this->success('',['detail' => $detail]);
    }

    public function goLike(Request $request){
        $user = $this->getUserId();
        $pid = $request->param('pid');
        $picLike = new PictureLike();
        $has = $picLike->where('user_id', $user['id'])->where('pid',$pid)->find();
        if(!empty($has)){
            $res = $picLike->where('user_id', $user['id'])->where('pid',$pid)->delete();
            if($res){
                $this->success('取消点赞成功');
            }else{
                $this->error('取消点赞失败');
            }
        }
        $picLike->pid = $pid;
        $picLike->user_id = $user['id'];
        $res = $picLike->save();
        if($res){
            $this->success('点赞成功');
        }else{
            $this->error('点赞失败');
        }
    }

    public function goCollection(Request $request){
        $user = $this->getUserId();
        $pid = $request->param('pid');
        $picLike = new PictureCollection();
        $has = $picLike->where('user_id', $user['id'])->where('pid',$pid)->find();
        if(!empty($has)){
            $res = $picLike->where('user_id', $user['id'])->where('pid',$pid)->delete();
            if($res){
                $this->success('取消收藏成功');
            }else{
                $this->error('取消收藏失败');
            }
        }
        $picLike->pid = $pid;
        $picLike->user_id = $user['id'];
        $res = $picLike->save();
        if($res){
            $this->success('收藏成功');
        }else{
            $this->error('收藏失败');
        }
    }

}
